package com.wwc.bwcj.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.wwc.bwcj.R
import com.wwc.bwcj.entity.CartProduct
import com.bumptech.glide.Glide
import com.wwc.bwcj.util.OnCartChangeListener

/**
 * 展示购物车中的商品列表并处理用户交互操作
 */
class CartAdapter(private var items: MutableList<CartProduct>, private val listener: OnCartChangeListener? = null) : RecyclerView.Adapter<CartAdapter.ViewHolder>() {

    // 购物车商品项的视图组件
    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val imgProduct: ImageView = itemView.findViewById(R.id.img_product)
        val tvName: TextView = itemView.findViewById(R.id.tv_name)
        val tvPrice: TextView = itemView.findViewById(R.id.tv_price)
        val tvSpec: TextView = itemView.findViewById(R.id.tv_spec)
        val btnMinus: Button = itemView.findViewById(R.id.btn_minus)
        val btnPlus: Button = itemView.findViewById(R.id.btn_plus)
        val tvQuantity: TextView = itemView.findViewById(R.id.tv_quantity)
        val ivDelete: ImageView = itemView.findViewById(R.id.iv_delete)
    }

    // 创建视图持有者
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_cart_product, parent, false)
        return ViewHolder(view)
    }

    // 数据绑定
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val item = items[position]
        // 根据商品名称映射对应的本地图片资源
        val imageResId = when (item.productName) {
            "伯牙绝弦" -> R.drawable.product_byjx
            "栀香柠" -> R.drawable.product_zxn
            "琥珀光" -> R.drawable.product_hpg
            "白雾红尘" -> R.drawable.product_bwhc
            "桂馥兰香" -> R.drawable.product_gflx
            "山野栀子" -> R.drawable.product_syzz
            "花田乌龙" -> R.drawable.product_htwl
            "万里木兰" -> R.drawable.product_wlml
            else -> R.drawable.product_byjx // 默认图片
        }
        holder.imgProduct.setImageResource(imageResId)

        // 显示商品名称、价格、规格和数量
        holder.tvName.text = item.productName
        holder.tvPrice.text = "¥${item.price}"
        holder.tvSpec.text = "${item.size}, ${item.ice}, ${item.sugar}"
        holder.tvQuantity.text = item.quantity.toString()

        // 减少按钮: 数量大于1时递减，更新UI并通知监听器
        holder.btnMinus.setOnClickListener {
            if (item.quantity > 1) {
                item.quantity--
                holder.tvQuantity.text = item.quantity.toString()
                notifyItemChanged(position)
                listener?.onCartChanged() // 通知数据变化
            }
        }

        // 增加按钮: 数量递增，更新UI并通知监听器
        holder.btnPlus.setOnClickListener {
            item.quantity++
            holder.tvQuantity.text = item.quantity.toString()
            notifyItemChanged(position)
            listener?.onCartChanged() // 通知数据变化
        }

        // 删除按钮: 从列表中移除商品，更新UI并通知监听器
        holder.ivDelete.setOnClickListener {
            items.removeAt(position)
            notifyItemRemoved(position)
            listener?.onCartChanged() // 通知数据变化
        }
    }

    // 获取购物车商品数量
    override fun getItemCount(): Int = items.size

    // 计算购物车商品总金额
    fun getTotalAmount(): Double {
        return items.sumByDouble { it.price * it.quantity }
    }
}
